// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-6-29 19:59:26
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   t_user_QueryMap.java

package com.creawor.hz_market.t_user;

import com.creawor.hz_market.servlet.LoadMapInfoAjax;
import com.creawor.hz_market.t_function.t_function;
import com.creawor.hz_market.t_role.t_role;
import com.creawor.hz_market.util.ComQuery;
import com.creawor.imei.base.AbsQueryMap;
import java.io.PrintStream;
import java.util.*;

import org.apache.log4j.Logger;

import net.sf.hibernate.*;

// Referenced classes of package com.creawor.hz_market.t_user:
//            t_user, t_user_Form

public class t_user_QueryMap extends AbsQueryMap
{
	private static final Logger logger = Logger.getLogger(LoadMapInfoAjax.class);
    public t_user_QueryMap()
        throws HibernateException
    {
        initSession();
    }

    public List findAll()
        throws HibernateException
    {
    	System.out.println("########################");
        Query query=null;
				try {
					totalrow = ((Integer)session.iterate("select count(*) from t_user").next()).intValue();
					String querystr = "from t_user";
					query = session.createQuery(querystr);
					setQueryPage(query);
				} catch (RuntimeException e) {
					System.out.println("########################");
					e.printStackTrace();
				}
        return query.list();
    }
    
    public Map getDepartmentUser(){
    	Map du=new HashMap();
    	String sql="select  dept_code,id,username,usercode,mobile from t_user group by dept_code,id,username,usercode,mobile having mobile is not null";
    	ComQuery query=new ComQuery();
    	List rs=query.find(sql);
    	if(null!=rs){
    	Iterator it=	rs.iterator();
    	Map row=null;
    	String dept_code=null;
    	while(it.hasNext()){
    		row=(Map)it.next();
    		dept_code=(String)row.get("dept_code");
    		List users=(List)du.get(dept_code);
    		if(null==users){
    			users=new ArrayList();
    			users.add(row);
    			du.put(dept_code, users);
    		}else{
    			users.add(row);
    		}
    	}
    	}
    	return du;
    }

    public t_user getByUsername(String username)
        throws HibernateException
    {
    	
    	t_user user=null;
    		String sql="select * from t_user where username='"+username+"'";
    		ComQuery query=new ComQuery();
      	List rs=query.find(sql);
      	if(null!=rs && rs.size()>0){
	      		user=new t_user();
	      		Map umap=(Map)rs.get(0);
	      		user.setId(Integer.parseInt(String.valueOf(umap.get("id"))));
	      		user.setUsercode(String.valueOf(umap.get("usercode")));
	      		user.setUsername(String.valueOf(umap.get("username")));
	      		user.setPassword(String.valueOf(umap.get("password")));
	      		user.setDepartment(String.valueOf(umap.get("department")));
	      		user.setDept_code(String.valueOf(umap.get("dept_code")));
	      		user.setEmail(String.valueOf(umap.get("email")));
	      		user.setMobile(String.valueOf(umap.get("mobile")));
	      		user.setCount(Integer.parseInt(String.valueOf(umap.get("count"))));
	      		user.setCounttotal(Integer.parseInt(String.valueOf(umap.get("counttotal"))));
	      		String roleSql="select * from t_role where id in(select role_id from t_user_role where user_id ="+user.getId()+")";
	      		List roles=query.find(roleSql);
	      		if(null!=roles && roles.size()>0){
	      			Set roleSet=new HashSet();
	      			Iterator rit=roles.iterator();
	      			while(rit.hasNext()){
	      				Map m=(Map)rit.next();
	      				t_role role=new t_role();
	      				role.setId(Integer.parseInt(String.valueOf(m.get("id"))));
	      				role.setRole_name(String.valueOf(m.get("role_name")));
	      				role.setDescription(String.valueOf(m.get("description")));
	      				setRoleFun(role);
	      				roleSet.add(role);
	      			}
	      			user.setRoles(roleSet);
	      		}
	      		
      		}
      	t_user user1= (t_user)session.createQuery("from t_user as a where a.username = :username").setString("username", username).uniqueResult();
      	
      
      	
      	return user;
    }
    
    private void setRoleFun(t_role role){
    	if(null!=role){
    		String sql="select r.role_name,r.description,rf.functionid ,f.functionname,f.url,f.descption from t_role r,t_role_function rf,t_function f where r.id=rf.roleid and rf.functionid=f.id and r.id="+role.getId()+"'";
    		ComQuery query=new ComQuery();
    		List ls=query.find(sql);
    		Iterator it=ls.iterator();
    		Map row=null;
    		Set funset=new HashSet();
    		while(it.hasNext()){
    			row=(Map)it.next();
    			t_function fun=new t_function();
    			fun.setId(Integer.parseInt(String.valueOf(row.get("functionid"))));
    			fun.setFunctionname(String.valueOf(row.get("functionname")));
    			fun.setUrl(String.valueOf(row.get("url")));
    			fun.setDescption(String.valueOf(row.get("descption")));
    			funset.add(fun);
    		}
    		role.setFunctions(funset);
    	}
    }

    public List getUsersBySql(String sql){
    	System.out.println("sql="+sql);
    	ComQuery query=new ComQuery();
    	List rs=query.find(sql);
    	return rs;
    }
    public t_user_Form getByID(String ID)
        throws HibernateException
    {
    	
    	t_user_Form user=null;
    		String sql="select * from t_user where id='"+ID+"'";
    		ComQuery query=new ComQuery();
      	List rs=query.find(sql);
      	if(null!=rs && rs.size()>0){
	      		user=new t_user_Form();
	      		Map umap=(Map)rs.get(0);
	      		//user.setId(Integer.parseInt(String.valueOf(umap.get("id"))));
	      		user.setId(String.valueOf(umap.get("id")));
	      		user.setUsercode(String.valueOf(umap.get("usercode")));
	      		user.setUsername(String.valueOf(umap.get("username")));
	      		user.setPassword(String.valueOf(umap.get("password")));
	      		user.setDepartment(String.valueOf(umap.get("department")));
	      		user.setDept_code(String.valueOf(umap.get("dept_code")));
	      		user.setEmail(String.valueOf(umap.get("email")));
	      		user.setMobile(String.valueOf(umap.get("mobile")));
//	      	user.setCount(Integer.parseInt(String.valueOf(umap.get("count"))));
	      		user.setCount(String.valueOf(umap.get("count")));
//	      	user.setCounttotal(Integer.parseInt(String.valueOf(umap.get("counttotal"))));
	      		user.setCounttotal(String.valueOf(umap.get("counttotal")));
	      		
	      	
      		}
     
      	
      	return user;
    }

    public t_user_Form getByIDv0(String ID)
    throws HibernateException
{
    t_user_Form vo = null;
    //System.out.println((new StringBuilder("\nt_user_QueryMap getByID:")).append(ID).toString());
    logger.debug((new StringBuilder("\nt_user_QueryMap getByID:")).append(ID).toString());
    session.clear();
    try
    {
        vo = new t_user_Form();
        t_user po = (t_user)session.load(t_user.class, new Integer(ID));
        try
        {
            vo.setId(String.valueOf(po.getId()));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        vo.setUsername(po.getUsername());
        vo.setPassword(po.getPassword());
        vo.setDept_code(po.getDept_code());
        vo.setUsercode(po.getUsercode());
        vo.setMobile(po.getMobile());
        vo.setEmail(po.getEmail());
        vo.setCount(String.valueOf(po.getCount()));
        vo.setCounttotal(String.valueOf(po.getCounttotal()));

        List role_ids = new ArrayList();
        t_role role;
        for(Iterator it = po.getRoles().iterator(); it.hasNext(); role_ids.add(String.valueOf(role.getId())))
            role = (t_role)it.next();

        vo.setRole_id((String[])role_ids.toArray(new String[0]));
    }
    catch(HibernateException e)
    {
       // System.out.println((new StringBuilder("\nERROR in getByID @t_user:")).append(e).toString());
        logger.debug((new StringBuilder("\nERROR in getByID @t_user:")).append(e).toString());
    }
    return vo;
}
    static Class class$0;
}